#定义元素和操作元素的方法
from selenium.webdriver.common.by import By
import time
from Base.basePage import BasePage


class OrderPage(BasePage):

    #元素
    payBtn=(By.XPATH,"/html/body/div[1]/div[1]/div[3]/div[3]/div/div[3]/form/div[2]/button")   #批量支付
    search=(By.XPATH,"/html/body/div[1]/div[1]/div[3]/div[3]/div/div[3]/form/div[3]/table/thead/tr/th[2]/div/input")   #搜索
    orderState=(By.XPATH,"/html/body/div[1]/div[1]/div[3]/div[3]/div/div[3]/form/div[3]/table/thead/tr/th[3]/div/div[2]/ul/li/input")  #订单状态
    payState=(By.XPATH,"/html/body/div[1]/div[1]/div[3]/div[3]/div/div[3]/form/div[3]/table/thead/tr/th[4]/div/div[2]/ul/li/input")   #支付状态
    detail=(By.XPATH,"/html/body/div[1]/div[1]/div[3]/div[3]/div/div[3]/form/div[3]/table/tbody/tr[2]/td[31]/a/span")  #详情
    cancel=(By.XPATH,"/html/body/div[1]/div[1]/div[3]/div[3]/div/div[3]/form/div[3]/table/tbody/tr[2]/td[31]/button[1]/span")  #取消
    form=(By.XPATH,"/html/body/div[1]/div[1]/div[3]/div[3]/div/div[3]/form/div[3]/table/tbody/tr[2]/td[31]/button[2]/span")   #表单
    repurchase=(By.XPATH,"/html/body/div[1]/div[1]/div[3]/div[3]/div/div[3]/form/div[3]/table/tbody/tr[2]/td[31]/div/span/span")  #回购
    refund=(By.XPATH,"/html/body/div[1]/div[1]/div[3]/div[3]/div/div[3]/form/div[3]/table/tbody/tr[2]/td[2]/div[2]/div/a")   #退款退货
    pagination=(By.XPATH,"/html/body/div[1]/div[1]/div[3]/div[3]/div/div[3]/div[2]/ul/li[4]")    #分页
    paginationText=(By.XPATH,"/html/body/div[1]/div[1]/div[3]/div[3]/div/div[3]/div[2]/ul/span[1]/input")   #输入每页条数
    cancelPage=(By.XPATH,"/html/body/div[21]/div/div[1]")
    refuseCancel=(By.XPATH,"/html/body/div[21]/div/div[3]/span[1]")
    formPage=(By.XPATH,"/html/body/div[22]/div/div[1]/h4")
    refuseForm=(By.XPATH,"/html/body/div[22]/div/div[1]/span")
    repurchasePage=(By.XPATH,"/html/body/div[1]/div[1]/div[3]/div[3]/div/div[3]/form/div[3]/table/tbody/tr[2]/td[31]/div/ul/div")
    payPage=(By.XPATH,"/html/body/div[9]/div/div[1]/h4")
    refusePay=(By.XPATH,"/html/body/div[9]/div/div[2]/form/div[2]/button[1]")
    paySelect=(By.XPATH,"/html/body/div[1]/div[1]/div[3]/div[3]/div/div[3]/form/div[3]/table/tbody/tr[1]/td[1]/div/label/span/i[2]")


    #操作元素的方法
    def __init__(self, driver):
        super().__init__(driver)
        self.driver = driver
        self.url = "https://d2.shopxo.vip/order/index.html"

    def open(self,cookie):
        """打开购物车页面页面"""
        self.driver.get(self.url)
        self.driver.add_cookie(cookie_dict=cookie)
        # self.click_element(self.cartBtn)
        self.wait_for_element(self.payBtn)
        return self

    def is_on_order_page(self):
        """检查是否在订单页面"""
        return "我的订单" in self.driver.title

    def click_pay_and_correct(self):
        """点击支付并确认"""
        self.click_element(self.paySelect)
        self.click_element(self.payBtn)
        self.is_element_present(self.payPage)
        self.click_element(self.refusePay)
        return self
    def click_detail(self):
        """点击详情"""
        self.click_element(self.detail)
        return self
    def is_detail(self):
        self.switch_to_new_window(10)
        assert "订单详情" in self.driver.title
        self.switch_to_main_window()
        return self

    def click_cancel_and_correct(self):
        """点击取消并确认"""
        self.click_element(self.cancel)
        self.is_element_present(self.cancelPage)
        self.click_element(self.refuseCancel)
        return self
    def click_form_and_correct(self):
        """点击表单并确认"""
        self.click_element(self.form)
        self.is_element_present(self.formPage)
        self.click_element(self.refuseForm)
        return self
    def click_repurchase_and_correct(self):
        """点击回购并确认"""
        self.click_element(self.repurchase)
        self.is_element_present(self.repurchasePage)
        self.click_element(self.repurchase)
        return self
    def click_refund(self):
        """点击退款退货"""
        self.click_element(self.refund)
        return self
    def is_refund(self):
        self.switch_to_new_window(10)
        assert '订单售后详情' in self.driver.title
        self.switch_to_main_window()
    def click_pagination_and_correct(self):
        """点击分页并确认"""
        self.input_text(self.paginationText,1)
        current_page = self.pagination.text
        self.click_element(self.pagination)
        new_page=self.pagination.text
        assert current_page+1 == new_page
        return self



